package cn.simon.service.Impl;

import cn.simon.mapper.BiliUserMapper;
import cn.simon.mapper.BiliUserdetailMapper;
import cn.simon.mapper.BiliUserrelatedMapper;
import cn.simon.model.BiliUser;
import cn.simon.model.BiliUserdetail;
import cn.simon.model.BiliUserrelated;
import cn.simon.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class LoginServiceImpl {
    private SqlSessionUtil sqlSessionUtil;
    private SqlSession sqlSession;
    private BiliUserMapper userMapper;
    private BiliUserdetailMapper userdetailMapper;
    private BiliUserrelatedMapper userrelatedMapper;

    public  LoginServiceImpl(){
        sqlSession = SqlSessionUtil.getSqlSession();
        userMapper = sqlSession.getMapper(BiliUserMapper.class);
        userdetailMapper = sqlSession.getMapper(BiliUserdetailMapper.class);
        userrelatedMapper = sqlSession.getMapper(BiliUserrelatedMapper.class);
    }

    public BiliUser getPaword(String username){
        BiliUser user = userMapper.selectByName(username);
        return user;
    }
    public int addBiliUser(BiliUser biliUser){
        int i = 0;
        try{
            i =   userMapper.insert(biliUser);
            BiliUserdetail biliUserdetail = BiliUserdetail.builder()
                    .biliuserId(biliUser.getBiliuserId())
                    .build();
            userdetailMapper.insert(biliUserdetail);
            BiliUserrelated biliUserrelated = BiliUserrelated.builder()
                    .biliuserId(biliUser.getBiliuserId())
                    .biliuserCoins(100)
                    .biliuserCollect(0)
                    .biliuserPost(0)
                    .biliuserLikes(0)
                    .build();
            userrelatedMapper.insert(biliUserrelated);
        }catch (Exception e){
            sqlSession.rollback();
        }
        sqlSession.commit();
        return i;
    }
}
